//
//  WSCreateAlter.m
//  CharonTVProject
//
//  Created by karong on 2023/6/21.
//

#import "WSCreateAlter.h"
#import <WebKit/WKWebView.h>
#import <WebKit/WKPreferences.h>
#import <WebKit/WKWebViewConfiguration.h>

@interface WSCreateAlter () <UIGestureRecognizerDelegate,WKUIDelegate,WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) WKWebViewConfiguration *config;

@end

@implementation WSCreateAlter

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addWeb];
}

- (void)addWeb{
    self.config = [[WKWebViewConfiguration alloc] init];
    self.config.preferences.javaScriptEnabled = YES;
    self.config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    self.webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:self.config];
    self.webView.backgroundColor = self.view.backgroundColor;
    self.webView.allowsBackForwardNavigationGestures = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mainView).offset(27);
        make.left.mas_equalTo(self.mainView).offset(23);
        make.right.mas_equalTo(self.mainView).offset(-23);
        make.bottom.mas_equalTo(self.confirmBtn.mas_top).offset(-23);
    }];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:FULL_ADDRESS(self.charonTV_url)]];
    [self.webView loadRequest:request];
}

- (IBAction)confirmAction:(id)sender {
    [self dismiss];
}

#pragma mark
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    /// 延时0.2s 显示网页
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showWebView];
    });
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    self.webView.hidden = NO;
}

- (void)showWebView{
    self.webView.hidden = NO;

    for (UIView* subview in self.webView.subviews) {
        for (UIGestureRecognizer* longPress in subview.gestureRecognizers) {
            if ([longPress isKindOfClass:UILongPressGestureRecognizer.class]) {
                [subview removeGestureRecognizer:longPress];
            }
        }
    }
}
@end
